package com.cyk.comicforumjava.demos.service.impl;

import com.cyk.comicforumjava.demos.base.ResponseResult;
import com.cyk.comicforumjava.demos.dto.SectionDto;
import com.cyk.comicforumjava.demos.dto.UserDto;
import com.cyk.comicforumjava.demos.dto.ZoneDto;
import com.cyk.comicforumjava.demos.enits.*;
import com.cyk.comicforumjava.demos.mapper.*;
import com.cyk.comicforumjava.demos.service.ManageService;
import com.cyk.comicforumjava.demos.utils.IdProcess;
import com.cyk.comicforumjava.demos.vo.MessageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ManageServiceimpl implements ManageService {

    @Autowired

    LoginMapper loginMapper;
    @Override
    public ResponseResult getUserList(String userName) {
        List<User>userList=loginMapper.selecAll(userName);
        return ResponseResult.success(userList);
    }


    @Override
    public ResponseResult banUser(Long id) {
        UserDto userDto=new UserDto();
        userDto.setId(id);
        User user=loginMapper.selectByUserId(userDto.getId());
        log.info("User:"+userDto);
        System.out.println("111");
        user.setBan(1);
        log.info("User:"+user);
        loginMapper.updateUser(user);
        return ResponseResult.success("操作成功");


    }

    @Override
    public ResponseResult unBanUser(Long id) {

        try {
            User user=loginMapper.selectByUserId(id);
            user.setBan(0);
            loginMapper.updateUser(user);
        }catch (Exception e){
            return ResponseResult.error("操作失败");
        }

        return ResponseResult.success("操作成功");
    }

    @Autowired
    PostMapper postMapper;
    @Override
    public ResponseResult getPostList(String postContent) {
        List<Post>postList=postMapper.selectAll(postContent);
        for (Post post:postList){
            post.setUser(loginMapper.selectByUserId(post.getPostMaster()));
        }
        return ResponseResult.success(postList);
    }

    @Override
    public ResponseResult deletePost(String id) {

        postMapper.deletePost(id);
        return ResponseResult.success("操作成功");
    }

    @Override
    public ResponseResult getCommentList(String commentContent) {
        List<Comment>commentList=postMapper.selectAllComment(commentContent);
        return ResponseResult.success(commentList);
    }

    @Override
    public ResponseResult deleteComment(String commentId) {
        postMapper.deleteComment(commentId);
        return ResponseResult.success("操作成功");
    }

    @Autowired
    MessageMapper messageMapper;
    @Override
    public ResponseResult getChatList(String content) {
        List<MessageVo>messageList=messageMapper.searchMessage(content);
        for (MessageVo message:messageList){
            message.setSenderName(loginMapper.selectByUserId(message.getSenderId()).getUsername());

        }
        return ResponseResult.success(messageList);
    }

    @Override
    public ResponseResult deleteMessage(String id) {
        messageMapper.deleteMessage(id);
        return ResponseResult.success("操作成功");
    }
    @Autowired
    ZoneMapper zoneMapper;

    @Override
    public ResponseResult getZoneList(ZoneDto zoneDto) {
        List<Zone>zoneList=zoneMapper.allZone(zoneDto);
        return ResponseResult.success(zoneList);
    }

    @Override
    public ResponseResult addZone(ZoneDto zoneDto) {
        Zone zone=new Zone();
        zone.setId(IdProcess.generateZoneId());
        zone.setName(zoneDto.getName());
        zone.setMaster(zoneDto.getMaster());
        zoneMapper.addZone(zone);
        return ResponseResult.success("操作成功");
    }

    @Override
    public ResponseResult deleteZone(ZoneDto zoneDto) {
        zoneMapper.deleteZone(zoneDto);
        return ResponseResult.success("操作成功");
    }

    @Autowired
    SectionMapper sectionMapper;
    @Override
    public ResponseResult addSection(SectionDto sectionDto) {
        Section section=new Section();
        section.setSectionId(IdProcess.generateSectionId());
        section.setSectionName(sectionDto.getSectionName());
        section.setIntroduce(sectionDto.getIntroduce());
        section.setZoneId(sectionDto.getZoneId());
        section.setImg(sectionDto.getImg());
        section.setSectionMaster(sectionDto.getSectionMaster());
        sectionMapper.addSection(section);
        return ResponseResult.success("操作成功");
    }

    @Override
    public ResponseResult deleteSection(SectionDto sectionDto) {
        sectionMapper.deleteSection(sectionDto);
        return ResponseResult.success("操作成功");
    }


}
